//
//  PopTipView.m
//  YunGui
//
//  Created by Hanen 3G 01 on 16/4/25.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "PopTipView.h"

#import "SQButton.h"

@interface PopTipView ()
{
  
}


@end

@implementation PopTipView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderColor = [UIColor colorWithWhite:0.7 alpha:0.8].CGColor;
        self.layer.borderWidth = 0.8;
        self.layer.cornerRadius = 8.0f;
        self.layer.masksToBounds = YES;
//        self.layer.shouldRasterize = YES;
        
        [self addSubview:self.tipsLabel];
        [self addSubview:self.leftBtn];
        [self addSubview:self.rightBtn];
        
        [self addSubview:self.leftButton];
        [self addSubview:self.rightButton];
//        [self addSubviews];
    }
    return self;
}

- (UILabel *)tipsLabel
{
    if (!_tipsLabel) {
        _tipsLabel = [ViewTool getLabelWith:CGRectMake(10, 20, self.width - 20.0f, 50.0f) WithTitle:nil WithFontSize:15.0f WithTitleColor:BLACKCOLOR WithTextAlignment:NSTextAlignmentLeft];
        _tipsLabel.backgroundColor = [UIColor whiteColor];
        _tipsLabel.numberOfLines = 0;
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
        _tipsLabel.center = CGPointMake(self.width / 2.0f, (self.height - self.leftBtn.height ) / 2.0f);
        
       
    }
    
    return _tipsLabel;
}
- (SQButton *)leftBtn
{
    if (!_leftBtn) {
        CGFloat btnW ;
        if (self.rightBtn.hidden == YES) {
            btnW = self.width;
        }else btnW = self.width / 2.0f;
        
        
        _leftBtn = [[SQButton alloc] initWithFrame:CGRectMake(0, self.height - 45.0f, (self.width - 1.0f) / 2.0f, 45.0f)];
//        _leftBtn.backgroundColor
       
        [_leftBtn setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
        _leftBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        
        
        [_leftBtn addTarget:self action:@selector(clickLeftBtn) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *line1 = [ViewTool getLineViewWith:CGRectMake(0, _leftBtn.y - 1, self.width, 1) withBackgroudColor:GrayLineColor];
        [self addSubview:line1];
        
        UIView *line2 = [ViewTool getLineViewWith:CGRectMake(_leftBtn.maxX ,_leftBtn.y , 1, _leftBtn.height) withBackgroudColor:GrayLineColor];
        [self addSubview:line2];
        
       
    }
    return _leftBtn;
}

- (void)setIsShowImage:(BOOL)isShowImage
{
    _isShowImage = isShowImage;
    if(isShowImage){
        [_leftBtn setImage:[UIImage imageNamed:@"weixuanze.png"] forState:UIControlStateNormal];
        [_rightBtn setImage:[UIImage imageNamed:@"weixuanze.png"] forState:UIControlStateNormal];
    }

}
- (void)setChangeIndex:(int)changeIndex
{
    _changeIndex = changeIndex;
    if (changeIndex == 1) {
         [_leftBtn setImage:[UIImage imageNamed:@"yixuanze.png"] forState:UIControlStateNormal];
    }else if(changeIndex == 2){
         [_rightBtn setImage:[UIImage imageNamed:@"yixuanze.png"] forState:UIControlStateNormal];
    }
    [self performSelector:@selector(dimiss) withObject:nil afterDelay:0.5];
}
- (void)setBackEnabled:(BOOL)BackEnabled
{
    _BackEnabled = BackEnabled;
    self.backView.enabled = BackEnabled;
}
- (void)setLefttext:(NSString *)lefttext
{
    _lefttext = lefttext;
     [_leftBtn setTitle:lefttext forState:UIControlStateNormal];
}

- (void)setRighText:(NSString *)righText
{
    _righText = righText;
     [_rightBtn setTitle:righText forState:UIControlStateNormal];
}

- (SQButton *)rightBtn
{
        if (!_rightBtn) {
            _rightBtn = [[SQButton alloc] initWithFrame:CGRectMake((self.width - 1.0f) / 2.0f, self.height - 45.0f, self.width / 2.0f, 45.0f)];
            //        _leftBtn.backgroundColor
            [_rightBtn setTitle:self.righText forState:UIControlStateNormal];
            [_rightBtn setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
            _rightBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
            
              [_rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
            
        }
        return _rightBtn;
}
- (void)clickLeftBtn
{
    if ([self.delegate respondsToSelector:@selector(clickLeftbtnWith:)]) {
//         [self.leftBtn setImage:[UIImage imageNamed:@"yixuanze.png"] forState:UIControlStateNormal];
            [self.delegate clickLeftbtnWith:self];
//        [self.leftBtn setImage:[UIImage imageNamed:@"yixuanze.png"] forState:UIControlStateNormal];
        }
  
   
}
- (void)clickRightBtn
{
    
    if ([self.delegate respondsToSelector:@selector(clickRightbtnWith:)]) {
//         [self.leftBtn setImage:[UIImage imageNamed:@"yixuanze.png"] forState:UIControlStateNormal];
        [self.delegate clickRightbtnWith:self];
      
    }

}
-(void)showToView:(UIView *)ToView
{
   _backView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.hidden = YES;
    _backView.alpha = 0;
    self.center = CGPointMake(kScreenWidth / 2.0f, kScreenHeight / 2.0f - 64);
//    backView.userInteractionEnabled = YES;
    [_backView addTarget:self action:@selector(handleTap) forControlEvents:UIControlEventTouchUpInside];
    [ToView addSubview:_backView];
    [ToView addSubview:self];

//    NSDictionary *dic = @{@"alert":self};

//    [ToView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[alert]|" options:0 metrics:0 views:dic]];
//    [ToView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[alert]|" options:0 metrics:0 views:dic]];
//    [ToView layoutIfNeeded];
    
//    [UIView animateWithDuration:0.1 animations:^{
        _backView.hidden = NO;
        _backView.alpha = 0.5;
//    } completion:^(BOOL finished) {
        //弹出的动画效果
        [self reboundEffectAnimationDuration:0.5];
//    }];
   
}

- (void)handleTap
{
//    [UIView animateWithDuration:0.2 animations:^{
////        if ([self.delegate  respondsToSelector:@selector(clickBackView)]) {
////            [self.delegate clickBackView];
////        }
//        [self removeFromSuperview];
//    } completion:^(BOOL finished) {
//    
////        backView.alpha = 0;
////        backView.hidden = YES;
////        [backView removeFromSuperview];
//    }];
    
    [self dimiss];
    
}
-(void)dimiss
{
    [_backView removeFromSuperview];
    [self removeFromSuperview];
}



- (UIButton *)leftButton{
    if (!_leftButton) {
        CGFloat btnW ;
        if (self.rightButton.hidden == YES) {
            btnW = self.width;
        }else btnW = self.width / 2.0f;
        
        
        _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, self.height - 45.0f, (self.width - 1.0f) / 2.0f, 45.0f)];
        //        _leftBtn.backgroundColor
        
        [_leftButton setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        
        
        [_leftButton addTarget:self action:@selector(clickLeftBtn) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *line1 = [ViewTool getLineViewWith:CGRectMake(0, _leftButton.y - 1, self.width, 1) withBackgroudColor:GrayLineColor];
        [self addSubview:line1];
        
        UIView *line2 = [ViewTool getLineViewWith:CGRectMake(_leftButton.maxX ,_leftButton.y , 1, _leftButton.height) withBackgroudColor:GrayLineColor];
        [self addSubview:line2];
        
        
    }
    return _leftButton;

}
- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [[UIButton alloc] initWithFrame:CGRectMake((self.width - 1.0f) / 2.0f, self.height - 45.0f, self.width / 2.0f, 45.0f)];
        //        _leftBtn.backgroundColor
        [_rightButton setTitle:self.righTitle forState:UIControlStateNormal];
        [_rightButton setTitleColor:BLACKCOLOR forState:UIControlStateNormal];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        
        [_rightButton addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _rightButton;
}
- (void)setLeftTitle:(NSString *)leftTitle
{
    _leftTitle = leftTitle;
    [_leftButton setTitle:leftTitle forState:UIControlStateNormal];
}

- (void)setRighTitle:(NSString *)righTitle
{
    _righTitle = righTitle;
    [_rightButton setTitle:righTitle forState:UIControlStateNormal];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
